/**
 * 工具脚本, 用于从mongodb数据库同步数据到sqlite中
 * */
var sqlite3 = require('sqlite3').verbose();
var jsonDict = require('../data/dict.json');
var config = require('../config.json');
var cursor = 0;
var db = new sqlite3.Database(process.cwd() + config.store.database.dbFile);
db.run("CREATE TABLE IF NOT EXISTS words (word TEXT PRIMARY KEY, content TEXT, scale INT default 0, phonetic TEXT)",function () {

  //INDEX should be CREATED
  //CREATE UNIQUE INDEX `primary` ON `words` (`word` )
  //CREATE INDEX `scale` ON `words` (`scale` )

  (function insert() {
    if(cursor < jsonDict.length) {
      var stmt = db.prepare("INSERT INTO words values(?,?,?,?)");
      delete jsonDict[cursor]._id;
      stmt.run(jsonDict[cursor].word, JSON.stringify(jsonDict[cursor]), jsonDict[cursor].scale || 0, jsonDict[cursor].phonetic.default, function () {
          cursor++;
          console.log(cursor);
          insert();
      });
      stmt.finalize();
    } else {
      db.close();
    }
  })();


});

